<template>
  <div class="container">
    <h1>{{ articleW.title }}</h1>
    <p>
      <span>{{ articleW.created_at }}</span>
      <span>lmb</span>
      <span>我的相册</span>
      {{ articleW.browse }}
    </p>
    <div
      class="content"
      v-html="articleW.content"
      @click="previewPicture($event)"
    ></div>
    <img
      v-for="(i, index) in imgs"
      :key="index"
      :src="images(i)"
      alt=""
      @click="large(images(i))"
    />
    <Masks
      ref="childRef"
      :bigPicture="bigPicture"
      :isMask="isMask"
      @changeMaskState="changeMaskState"
    />
  </div>
</template>

<script>
import { getArticle } from '../api/article'
import Masks from '@/components/Masks.vue'
export default {
  components: {
    Masks
  },
  // props: {
  //   articleW: {
  //     type: Object,
  //     default: () => { }
  //   },
  //   imgs: {
  //     type: Array,
  //     default: () => { }
  //   }
  // },
  data() {
    return {
      articleW: {},
      baseURL: this.baseURL,
      imgs: [],
      isMask: false,
      bigPicture: ''
    }
  },
  async created() {
    const res = await getArticle({ id: this.$route.query.id })
    this.articleW = res.data.data
    // 替换v-html中的图片属性img设置样式
    // this.articleW.content = this.articleW.content.replace(
    //   /<img/g,
    //   "<img style='max-width:80%;height:auto;margin:5px'"
    // );
    this.imgs = this.articleW.image && this.articleW.image.split(',')
    console.log(this.imgs)
  },
  computed: {
    images: function () {
      return function (img) {
        return img === '' ? '' : `${this.baseURL}/uploads/${img}`
      }
    }
  },
  methods: {
    previewPicture(e) {
      console.log(e)
      if (e.target.tagName === 'IMG') {
        // this.picturePreview.src = e.target.src;
        // this.picturePreview.visible = true;
        this.bigPicture = e.target.src
        this.isMask = true
        this.$refs.childRef.enlarge()
      }
    },
    large(v) {
      this.bigPicture = v
      this.isMask = true
      this.$refs.childRef.enlarge()
    },
    changeMaskState() {
      this.isMask = false
    }
  }
}
</script>

<style lang="scss" scoped>
@media only screen and (max-width: 480px) {
  body .container {
    padding: 20px 10px;
    h1 {
      font-size: 18px;
    }
    p {
      font-size: 13px;
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      span {
        margin-right: 10px;
      }
    }
    .content {
      ::v-deep img {
        margin: 5px;
        width: 96%;
        box-sizing: border-box;
        height: 150px;
      }
      ::v-deep p {
        padding: 2px 0;
      }
      ::v-deep li {
        margin-left: 10px;
      }
    }
    img {
      margin: 5px;
      width: 96%;
      box-sizing: border-box;
      height: 150px;
    }
  }
}
.container {
  background: #fff;
  padding: 20px;
  border-radius: 3px;
  word-wrap: break-word;
  word-break: break-all;
  h1 {
    font-size: 22px;
    margin-bottom: 20px;
  }
  p {
    margin-bottom: 20px;
    span {
      margin-right: 15px;
      position: relative;
      padding-left: 20px;
      line-height: 20px;
      cursor: pointer;
    }
    span:nth-child(1) {
      background: url(../assets/icon.png) no-repeat 0 2px;
      background-size: 18px;
    }
    span:nth-child(2) {
      background: url(../assets/icon.png) no-repeat 0 -16px;
      background-size: 18px;
    }
    span:nth-child(3) {
      background: url(../assets/icon.png) no-repeat 0 -34px;
      background-size: 18px;
      color: #518f97;
    }
    span:nth-child(n):before {
      position: absolute;
      content: '';
      width: 20px;
      height: 20px;
      left: 0;
      top: 0;
    }
  }
  .content {
    margin: 20px 0 10px;
    ::v-deep img {
      margin: 10px;
      box-sizing: border-box;
      width: 30%;
      height: 250px;
      object-fit: cover;
    }
    ::v-deep p {
      padding: 5px 0;
    }
    ::v-deep li {
      margin-left: 20px;
    }
  }
  img {
    margin: 10px;
    box-sizing: border-box;
    width: 30%;
    height: 230px;
    object-fit: cover;
  }
}
</style>
